package com.tcm.controller.backend;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.dto.ExercisePublishDto;
import com.tcm.entity.dto.param.ExercisePublishPageParam;
import com.tcm.entity.resp.R;
import com.tcm.service.ExerciseService;
import com.tcm.utils.AccessTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 实训练习管理控制器
 * @author Guqier
 * @version 1.0
 * @date 2023/8/10 21:35
 */
@RestController
@RequestMapping("/pc/exercise")
public class ExerciseController {

    @Autowired
    private ExerciseService exerciseService;

    /**
     * 发布练习
     * @param dto 数据封装
     * @param request 请求对象
     * @return 处理结果
     */
    @RequestMapping(method = RequestMethod.POST)
    public R publishExercise(@RequestBody @Valid ExercisePublishDto dto, HttpServletRequest request){
        if (CollectionUtils.isEmpty(dto.getClassIds())) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        return exerciseService.publishExercise(dto, AccessTokenUtil.getUserId(request)) ? R.success() : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

    /**
     * 取消发布练习
     * @param id 练习ID
     * @return 处理结果
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public R canselPublishExercise(@RequestParam Long id){
        return exerciseService.canselPublishExercise(id) ? R.success() : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

    /**
     * 教师用户分页获取发布练习列表
     * @param param 分页参数
     * @return 发布练习列表
     */
    @RequestMapping(method = RequestMethod.GET)
    public R getPublishExerciseListByPage(ExercisePublishPageParam param, HttpServletRequest request){
        if (param.getPage() == null || param.getPageSize() == null) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        return R.success(exerciseService.getPublishExerciseListByPage(param, AccessTokenUtil.getUserId(request)));
    }

    /**
     * 获取学生练习情况列表
     * @param id 练习ID
     * @return 学生练习情况列表
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public R getStudentCompletionStatusList(@RequestParam Long id){
        return R.success(exerciseService.getStudentCompletionStatusList(id));
    }

}
